1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 import java.io.*;
37 import java.rmi.*;
38 import java.rmi.activation.*;
39 import java.rmi.server.*;
40 import java.rmi.registry.*;
41 import java.util.*;
42
43 public class ForceLogSnapshot
44 implements ActivateMe
45 {
46
47 final public static int HOW_MANY = 50;
48 final public static int NUM_GROUPS = 4;
49
50 final public static int SNAPSHOT_INTERVAL = 10;
51
52 private ActivationID id;
53 private Vector responders = new Vector();
54
55 private static final String RESTARTABLE = "restartable";
56 private static final String ACTIVATABLE = "activatable";
57
58 private static Object lock = new Object();
59 private static boolean[] restartedObjects = new boolean[HOW_MANY];
60 private static boolean[] activatedObjects = new boolean[HOW_MANY];
61
62 public ForceLogSnapshot(ActivationID id, MarshalledObject mobj)
63 throws ActivationException, RemoteException
64 {
65 this.id = id;
66 int intId = 0;
67
68 Activatable.exportObject(this, id, 0);
69 ActivateMe obj;
70 String responder;
71 try {
72 Object[] stuff = (Object[]) mobj.get();
73
74 intId = ((Integer) stuff[0]).intValue();
75 responder = (String) stuff[1];
76 obj = (ActivateMe) stuff[2];
77
78 System.err.println(responder + " service started");
79 } catch (Exception e) {
80 System.err.println("unable to obtain stub from marshalled object");
81 System.err.println(e.getMessage());
82 e.printStackTrace();
83 return;
84 }
85
86 obj.ping(intId, responder);
87 }
88
89 public ForceLogSnapshot() throws RemoteException {
90 UnicastRemoteObject.exportObject(this, 0);
91 }
92
93 public void ping(int intId, String responder) {
94 System.err.println("ForceLogSnapshot: received ping from " +
95 responder);
96 if (responder.equals(RESTARTABLE)) {
97 synchronized (lock) {
98 restartedObjects[intId] = true;
99 }
100 } else if (responder.equals(ACTIVATABLE)) {
101 synchronized (lock) {
102 activatedObjects[intId] = true;
103 }
104 }
105 }
106
107 public void crash() {
108 System.exit(0);
109 }
110
111 public ActivationID getID() {
112 return id;
113 }
114
115 public static void main(String[] args) {
116
117 System.out.println("\nRegression test for bug 4173960\n");
118
119 TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
120
121 RMID rmid = null;
122 ForceLogSnapshot[] unicastObjs = new ForceLogSnapshot[HOW_MANY];
123
124 try {
125 String option = " -Dsun.rmi.activation.snapshotInterval=" +
126 SNAPSHOT_INTERVAL;
127
128 RMID.removeLog();
129 rmid = RMID.createRMID();
130 rmid.addOptions(new String[] {option, "-Djava.compiler="});
131 rmid.start();
132
133
134
135
136 Properties p = new Properties();
137
138
139 p.put("java.security.policy",
140 TestParams.defaultGroupPolicy);
141 p.put("java.security.manager",
142 TestParams.defaultSecurityManager);
143
144 Object[][] stuff = new Object[HOW_MANY][];
145 MarshalledObject restartMobj = null;
146 ActivationGroupDesc groupDesc = null;
147 MarshalledObject activateMobj = null;
148 ActivationGroupID[] groupIDs = new ActivationGroupID[NUM_GROUPS];
149 ActivationDesc restartableDesc = null;
150 ActivationDesc activatableDesc = null;
151 ActivateMe[] restartableObj = new ActivateMe[HOW_MANY];
152 ActivateMe[] activatableObj = new ActivateMe[HOW_MANY];
153
154
155
156
157 int group = 0;
158 int groupNo = 0;
159 for (int i = 0 ; i < HOW_MANY ; i ++ ) {
160
161 System.err.println("Creating descriptors and remote objects");
162
163 unicastObjs[i] = new ForceLogSnapshot();
164
165
166
167
168
169
170 stuff[i] = new Object[] { new Integer(i),
171 RESTARTABLE, unicastObjs[i] };
172 restartMobj = new MarshalledObject(stuff[i]);
173
174 stuff[i][1] = ACTIVATABLE;
175 activateMobj = new MarshalledObject(stuff[i]);
176
177 groupDesc =
178 new ActivationGroupDesc(p, null);
179
180 if (i < NUM_GROUPS) {
181 groupNo = i;
182 groupIDs[groupNo] =
183 ActivationGroup.getSystem().
184 registerGroup(groupDesc);
185 } else {
186 groupNo = (group++)%NUM_GROUPS;
187 }
188
189 System.err.println("Objects group number: " + groupNo);
190
191 restartableDesc =
192 new ActivationDesc(groupIDs[groupNo], "ForceLogSnapshot", null,
193 restartMobj, true);
194
195 activatableDesc =
196 new ActivationDesc(groupIDs[groupNo], "ForceLogSnapshot", null,
197 activateMobj, false);
198
199 System.err.println("Registering descriptors");
200 restartableObj[i] =
201 (ActivateMe) Activatable.register(restartableDesc);
202
203 activatableObj[i] =
204 (ActivateMe) Activatable.register(activatableDesc);
205 System.err.println("registered activatable #: " + i);
206
207
208 }
209
210 int repeatOnce = 1;
211 do {
212
213
214
215
216 rmid.restart();
217
218 if (howManyRestarted(restartedObjects, 10) < HOW_MANY) {
219 TestLibrary.bomb("Test1 failed: a service would not " +
220 "restart");
221 }
222 System.err.println("Test1 passed: rmid " +
223 "all service(s) restarted. Performing next test.");
224
225
226
227
228
229 if (howManyRestarted(activatedObjects, 2) != 0) {
230 TestLibrary.bomb("Test2 failed: activatable service restarted!",
231 null);
232 }
233 System.err.println("Test2 passed: rmid did not " +
234 "restart activatable service(s)");
235
236 if (repeatOnce > 0) {
237 try {
238 System.err.println("\nCrash restartable object");
239 for (int i = 0 ; i < HOW_MANY ; i ++) {
240 restartableObj[i].crash();
241 }
242 } catch (Exception e) {
243 }
244 }
245
246 } while (repeatOnce-- > 0);
247
248
249 } catch (Exception e) {
250 TestLibrary.bomb("test failed", e);
251 } finally {
252 ActivationLibrary.rmidCleanup(rmid);
253 for (int i = 0 ; i < HOW_MANY ; i ++) {
254 TestLibrary.unexport(unicastObjs[i]);
255 }
256 }
257 }
258
259
260
261
262
263 private static int howManyRestarted(boolean[] startedObjects, int retries) {
264 int succeeded = 0;
265 int restarted = 0;
266 int atry = 0;
267
268 while ((restarted < HOW_MANY) && (atry < retries)) {
269 restarted = 0;
270 for (int j = 0 ; j < HOW_MANY ; j ++ ) {
271 synchronized(lock) {
272 if (startedObjects[j]) {
273 restarted ++;
274 }
275 }
276 }
277 System.err.println("not all objects restarted, retrying...");
278 try {
279 Thread.sleep(10000);
280 } catch (InterruptedException ie) {
281 }
282 atry ++;
283 }
284 return restarted;
285 }
286 }